package mesweb.mesweb.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hszh.wms.mbg.PermissionManager.entity.UmsResource;
import com.hszh.wms.mbg.PermissionManager.mapper.UmsResourceMapper;
import mesweb.mesweb.service.UmsAdminCacheService;
import mesweb.mesweb.service.UmsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 后台资源管理Service实现类
 * Created by macro on 2020/2/2.
 */
@Service
public class UmsResourceServiceImpl implements UmsResourceService {
    @Autowired
    private UmsResourceMapper resourceMapper;
    @Autowired
    private UmsAdminCacheService adminCacheService;
    @Override
    public int create(UmsResource umsResource) {
        umsResource.setCreateTime(new Date());
        return resourceMapper.insert(umsResource);
    }

    @Override
    public int update(Long id, UmsResource umsResource) {
        umsResource.setId(id);
        int count = resourceMapper.updateByPrimaryKeySelective(umsResource);
        adminCacheService.delResourceListByResource(id);
        return count;
    }

    @Override
    public UmsResource getItem(Long id) {
        return resourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long id) {
        int count = resourceMapper.deleteByPrimaryKey(id);
        adminCacheService.delResourceListByResource(id);
        return count;
    }

    @Override
    public IPage<UmsResource> list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) {

        QueryWrapper<UmsResource> queryWrapper = new QueryWrapper<>();
        if(categoryId!=null){
            queryWrapper.eq("categoryId",categoryId);
        }
        if(StrUtil.isNotEmpty(nameKeyword)){
            queryWrapper.like("name",nameKeyword);
        }
        if(StrUtil.isNotEmpty(urlKeyword)){
            queryWrapper.like("url",urlKeyword);
        }
        queryWrapper.orderByAsc("id");
        IPage page = new Page(pageNum,pageSize);
        IPage<UmsResource> umsResources = resourceMapper.selectPage(page,queryWrapper);
        if(umsResources != null && umsResources.getRecords() != null)
        {
            return umsResources;
        }
        return null;
    }

    @Override
    public List<UmsResource> listAll() {
        return resourceMapper.selectList(null);
    }
}
